function roleCrudHandler(){ 
    
   
   $(IDS.list).jqGrid({
          url:router.index,
          datatype: "json",
          mtype: 'POST',
          colNames:['Azioni', 'Nome','Data creazione'],
          colModel:[
                    {name:'actions',width:80, sortable:false, editable:false,align:'center',search:false,formatter:actionsFormatter},
                    {name:'name',index:'name', width:200,editable:false},
                    {name:'createdAt',index:'createdAt', width:100,editable:false},    
               ],
          rowNum:3,
          rowList:[10,20,30],
          pager: jQuery(IDS.pager),
          sortname: 'createdAt',
          viewrecords: true,
          sortorder: "desc",
          caption:"Ruoli",
          height:"220px",
          width:"100%"
        
    }).jqGrid('navGrid',IDS.pager,{add:false,edit:false,del:false})
                 .navButtonAdd(IDS.pager,{
                    caption:"", 
                    buttonicon:"ui-icon-plus", 
                    onClickButton: function(){ 
                        createAction();
                    }, 
                    position:"first",
                    title:"Nuovo record"
                 });
   
};


function actionsFormatter(cellvalue) {
    var ocl = "onclick=editAction('" + cellvalue + "');";
    var str = "<div style='margin-left:8px;'><div title='Modifica' style='float:left;cursor:pointer;' class='ui-pg-div ui-inline-edit' " + ocl +">";
    str = str + "<span class='ui-icon ui-icon-pencil'></span></div>";
    
    ocl = "onclick=deleteAction('" + cellvalue + "');";
    str = str + "<div title='Elimina' style='float:left;margin-left:5px;;cursor:pointer;' class='ui-pg-div ui-inline-del' " + ocl +">";
    str = str + "<span class='ui-icon ui-icon-trash'></span></div></div>";
    return str;
}

function editAction(id) {
    var urlGet = router.index + id + '/edit'; 
    var urlPost = router.index + id + '/update'; 
    //alert( urlGet + ' ' + urlPost);
    formDetails(urlGet, urlPost, false);
}

function deleteAction(id) {
    var url = router.index + id + '/delete' ; 
    var result = confirm('Vuoi Eliminare definitivamente il record ?');
    if(result){
         $.post(url)
            .success(function(data){
                      console.log(data.result); 
                      $(IDS.list).jqGrid('setGridParam', {page:1, sortname:'createdAt', sortorder:'desc'});
                      $(IDS.list).trigger("reloadGrid");    
                    });    
    }
}

function createAction() {
    formDetails(router._new, router.create, true);
}

function formDetails(urlGet, urlPost, bCreate){
     
      $( IDS.formctr ).dialog({
                    autoOpen: false,
                    height: 300,
                    width: 500,
                    modal: true,
                    buttons: {
                            "Salva": function() {
                                // validate

                                $(".errorsummary").empty();
                                $(".errorsummary").removeClass("ui-state-highlight"); 
                                var idForm = "#" + $("form", this).attr("id");

                                var dlg = $(this);
                                $.post(urlPost, $(idForm).serialize(),
                                        function(data){
                                            $(IDS.formctr).html(data); 
                                        }
                                    ).success(function() {
                                          if($(".errorsummary").children("ul").size() > 0){
                                             $(".errorsummary").addClass("ui-state-highlight");    
                                          }else{
                                              if(bCreate){
                                                 $(IDS.list).jqGrid('setGridParam', {page:1, sortname:'createdAt', sortorder:'desc'}); 
                                              }  
                                              $(IDS.list).trigger("reloadGrid"); 
                                              dlg.dialog( "close" );
                                          }
                                    });
                                }
                            ,

                            "Annulla": function() {
                                $( this ).dialog( "close" );
                            }
                    },
                    close: function() {
                        //allFields.val( "" ).removeClass( "ui-state-error" );
                    }
    });          


    $.get(urlGet, function(data){
            $(IDS.formctr).html(data); 
            $(IDS.formctr).dialog( "open" );
    });

}


